// pages/info/info.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    citycode:'',
    conum:'',
    cityconadd:'',
    curenum:'',
    deathnum:'',
    hhh:'',
    timess:'',
    mtimess:'',
    chengshi:[],
    city_all:[],
    name:'北京',  //默认的地方
    z:'东城区',
    now:[],
    shishi:'实时数据',
    shishii:'目前情况',
    swiper:['yiqin.jpg','yiqing.jpg'],
    shuju:['确诊人数','新增人数','治愈人数','死亡人数'],
    result: "",
    day:['今天','明天','后天','预测','预测','预测','预测'],
    icon:"",
    fengxiang:"",
    jiangshuilang:"",
    shidu:"",
    fengli:"",
    fengsu:"",
    chakan:false,
    region:"",
  },

  //周浩


  go:function(e){
    wx.navigateTo({
      url: '../kangjiyiqin/kangjiyiqin'
    })
  },
  panduan:function(e){
    if(app.globalData.sheng=='广东'){
      this.setData({hhh:'zhong.png'})
    }else if(app.globalData.sheng=='台湾'){
      this.setData({hhh:'gaofengxian.png'})
    }else{
      this.setData({hhh:'difengxian.png'})
    }
  },
bindchange:function(e){
 this.setData({now:e.detail.value})
 //console.log(this.data.now[0].substring(0,2))//获取的是北京区，需要的是北京.
 //console.log(this.data.now[1].substring(0,2))
 this.setData({now1:""})
 this.giao()
},
giao:function(e){
  this.panduan()
  var that=this;
    wx.request({
      url: 'https://interface.sina.cn/news/wap/fymap2020_data.d.json',
      method:'POST',
      header:{'Content-type':'application/x-www-form-urlencoded'},
      success:function(arg){
        //console.log(arg);
        that.setData({chengshi:arg.data.data.list})
        that.setData({timess:arg.data.data.times})
        that.setData({mtimess:arg.data.data.mtime})
        app.globalData.time=that.data.mtimess;
        //console.log(that.data.chengshi)
        //console.log(app.globalData.sheng)
        for(var i=0,len=that.data.chengshi.length;i<len;i++){
          if(that.data.chengshi[i].name==app.globalData.sheng){
            //console.log(arg.data.data.list[i].city)//根据省获得了城市的信息
            that.setData({city_all:arg.data.data.list[i].city})//把省内的所以城市赋值给city_all
            //console.log(that.data.city_all)
          }
        }
        for(var i=0,len=that.data.city_all.length;i<len;i++){
          if(that.data.city_all[i].name==app.globalData.shi){
            that.setData({citycode:that.data.city_all[i].citycode})
            that.setData({conum:that.data.city_all[i].conNum})
            that.setData({cityconadd:that.data.city_all[i].conadd})
            that.setData({curenum:that.data.city_all[i].cureNum})
            that.setData({deathnum:that.data.city_all[i].deathNum})
          }
        }
      },
      fail:function(err){
        //console.log("SORRY")
      },
      complete(com){
        wx.hideLoading()
      }
    })
},

//周浩





  //pinker位置
  getlocation(e){
    var qu = e.detail.value[2]
    var shi = e.detail.value[1]
    shi = shi.slice(0,shi.length-1)
    //console.log(shi)
    var sheng = e.detail.value[0]
    sheng = sheng.slice(0,sheng.length-1)
    // console.log(location)
    app.globalData.qu = qu
    app.globalData.shi = shi
    app.globalData.sheng = sheng
    this.setData({region:qu})
    this.giao()
    this.request()
  },

//请求天气
  request: function(e){
    var that = this
    // console.log(that.data.region)
    wx.request({
      url: 'https://v2.alapi.cn/api/weather/forecast',
      data: {token:'nQLzavbaLgo7j11A', location:app.globalData.qu},
      method: 'POST',
      header: {'content-type': 'application/x-www-form-urlencoded'},
      success:function(arg){
        var weather = arg.data.data.daily_forecast[0].cond_code_d
        //console.log(arg)
        //console.log(weather)//温度
        var result = arg.data.data.daily_forecast
        that.setData({icon:weather,result:result})
        that.setData({shidu:result[0].hum})
        that.setData({jiangshuiliang:result[0].pcpn})
        that.setData({fengxiang:result[0].wind_dir})
        that.setData({fengli:result[0].wind_sc})
        that.setData({fengsu:result[0].wind_spd})
      },
      fail: function(arg){
        console.log("ERR")
      }
    })
  },
  
  //获取定位
  sendMapRequest:function() {
    const that = this
    //console.log(that.data.latitude)
    //console.log(that.data.longitude)
    wx.request({
      url:
          'https://apis.map.qq.com/ws/geocoder/v1/' + "?location=" + app.globalData.latitude+ ',' +
          app.globalData.longitude + "&key=" + '	NZ4BZ-NHJKX-HCX42-THIFM-FPEQ6-XAB2E' + "&get_poi=1",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method:'GET',
      success: (res) => {
        if (res.statusCode == 200 && res.data.status == 0) {
          //console.log(res)
          //console.log(res.data.result.ad_info)
          var shi = res.data.result.ad_info.city
          var sheng = res.data.result.ad_info.province
          shi = shi.slice(0,shi.length-1)
          sheng = sheng.slice(0,sheng.length-1)
          app.globalData.shi = shi
          app.globalData.sheng = sheng
          //console.log(sheng)
          app.globalData.qu = res.data.result.ad_info.district
          that.setData({region:app.globalData.qu})
          that.panduan()
          that.giao()
          that.request()
        }
      }
    })
   
  },

  chakanxiangqing(e){
    this.setData({chakan: !this.data.chakan})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        app.globalData.latitude = latitude
        app.globalData.longitude = longitude
        // console.log(that.data.latitude)
        that.sendMapRequest()
        // console.log(region)
      }
     })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    // console.log(app.globalData.result)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})